<?php

// +----------------------------------------------------------------------
// | H1CMS © OpenSource CMS
// +----------------------------------------------------------------------
// | Copyright (c) 2014-2016 http://www.h1cms.com All rights reserved.
// | Copyright (c) 2014-2016 嘉兴领格信息技术有限公司，并保留所有权利。
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Allen <allen@lg4.cn>
// +----------------------------------------------------------------------

namespace h1cms\model;

use h1cms\system\Model;
use Lg\Database\Query;

/**
 * Node
 *
 * @author Allen <allen@lg4.cn>
 * 
 * @property int $node_id Node Id
 */
class Node extends Model {

    public $table = 'node';
    public $primaryKey = 'node_id';

    public static function getAllLanguages($node_id) {
        $languages = NodeLanguage::findAll([
                    'node_id' => $node_id
        ]);
        $data = [];
        foreach ($languages as $lang) {
            $data[$lang->lang_id] = $lang;
        }
        return $data;
    }

    public static function getAll($filters = []) {
        $query = Query::table('node as n')
                ->leftJoin('node_language as nl', "n.node_id=nl.node_id")
                ->where('nl.lang_id', array_get($filters, 'lang_id', 1));
//        $query = "select * from node as n left join node_language as nl";
        if (array_key_exists('node_id', $filters)) {
            $query->where('nl.node_id', $filters['node_id']);
        }
        if (array_key_exists('pagesize', $filters)) {
            $query->limit(array_get($filters, 'pagesize', 20), array_get($filters, 'offset', 0));
        }
        if (array_key_exists('order_by', $filters)) {
            $query->orderBy(array_get($filters, 'order_by'));
        }
        
        return $query->getAll();
    }

}
